package self.Lunastic.HexaSweeper.Manager;

import self.Lunastic.HexaSweeper.Object.GameArea;
import self.Lunastic.HexaSweeper.Object.GameTimer;

/**
 * 所有游戏流程控制 及全局数据存储 <br><br>
 * $gameArea: 雷区数据
 */
public class GameManager {
    // TODO 版本信息
    public static final String VERSION = "v1.0.1";
    public static final String CREATOR = "Lunastic";
    // 限制更新速度
    // tick per second
    public static final int TPS = 60;

    // 雷区大小
    private static final int MAX_SWEEPER_SIZE = 16;
    private static final int MIN_SWEEPER_SIZE = 3;
    private static int _sweeperSize = 12;
    public static void setSweeperSize(int sweeperSize) { GameManager._sweeperSize = sweeperSize; }
    public static int getSweeperSize() { return _sweeperSize; }
    public static void addSweeperSize(){ _sweeperSize = Math.max(MIN_SWEEPER_SIZE, Math.min(MAX_SWEEPER_SIZE, ++_sweeperSize)); }
    public static void minusSweeperSize(){ _sweeperSize = Math.max(MIN_SWEEPER_SIZE, Math.min(MAX_SWEEPER_SIZE, --_sweeperSize)); }

    // 炸弹占比
    private static final int MAX_BOMBER_PERCENT = 50;
    private static final int MIN_BOMBER_PERCENT = 10;
    private static int _bomberPercent = 20;
    public static void setBomberPercent(int bomberPercent) { GameManager._bomberPercent = bomberPercent; }
    public static int getBomberPercent() { return _bomberPercent; }
    public static void addBomberPercent(){ _bomberPercent = Math.max(MIN_BOMBER_PERCENT, Math.min(MAX_BOMBER_PERCENT, ++_bomberPercent)); }
    public static void minusBomberPercent(){ _bomberPercent = Math.max(MIN_BOMBER_PERCENT, Math.min(MAX_BOMBER_PERCENT, --_bomberPercent)); }

    // 用时
    public static final GameTimer $gameTime = new GameTimer();

    // 各种计数
    public static int coverCount = 0;
    public static int bomberCount = 0;
    public static int flagCount = 0;

    // 雷区数据
    public final static GameArea $gameArea = new GameArea();

    /**
     * <b>gameStatus</b>: 游戏状态标识<br>
     *  <ul><li>0：准备就绪</li><li>1：开始</li><li>2：失败</li><li>3：完成</li><ul/>
     */
    // 游戏状态
    public static final int GAME_RESET = 0;
    public static final int GAME_START = 1;
    public static final int GAME_FAILED = 2;
    public static final int GAME_SUCCEED = 3;
    private static int _gameStatus = 0;
    public static int getGameStatus(){return _gameStatus;}
    public static boolean isGameReset(){return _gameStatus == GAME_RESET;}
    public static boolean isGameStart(){return _gameStatus == GAME_START;}
    public static boolean isGameFailed(){return _gameStatus == GAME_FAILED;}
    public static boolean isGameSucceed(){return _gameStatus == GAME_SUCCEED;}

    // 游戏暂停
    private static boolean _gamePause = false;
    public static boolean isGamePause() { return GameManager._gamePause;}

    // 重置
    public static void reset(){
        int ss = GameManager.getSweeperSize();
        int bp = GameManager.getBomberPercent();
        GameManager.coverCount = 3*ss*ss-3*ss+1;
        GameManager.bomberCount = GameManager.coverCount*bp/100;
        GameManager.coverCount -= GameManager.bomberCount;
        GameManager.flagCount = GameManager.bomberCount;
        GameManager._gameStatus = GAME_RESET;
        GameManager.$gameTime.reset();

        $gameArea.areaInit();
        $gameArea.createCeil();
        FrameManager.resetWindowGame();
        System.gc();
    }

    public static void start(){
        GameManager._gameStatus = GAME_START;
        GameManager.$gameTime.run();
    }

    // 游戏结束
    public static void failed() {
        System.gc();
        GameManager._gameStatus = GAME_FAILED;
        GameManager.$gameTime.stop();
        FrameManager.stopWindowGame();
    }

    // 游戏成功
    public static void succeed() {
        System.gc();
        GameManager._gameStatus = GAME_SUCCEED;
        GameManager.$gameTime.stop();
        FrameManager.stopWindowGame();
    }

    // 游戏暂停
    public static void paused(){
        if(GameManager.isGameStart()) {
            GameManager._gamePause = true;
            GameManager.$gameTime.stop();
            FrameManager.framePaused();
        }
    }

    // 游戏继续
    public static void continued(){
        if(GameManager.isGameStart()) {
            GameManager._gamePause = false;
            GameManager.$gameTime.run();
            FrameManager.frameContinued();
        }
    }
}
